package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.Task;
import com.hebeu.gfh.pojo.vo.appinfo;
import com.hebeu.gfh.pojo.vo.publishedTask;
import com.hebeu.gfh.service.taskService;
import com.hebeu.gfh.service.userService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/task")
public class taskController {
    @Autowired
    taskService taskService;
    @Autowired
    userService userService;
    //发布任务
    @PostMapping("/publishTask")
    public R publishTask(Task task){
        try {
            taskService.publishTask(task);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("--------------------");
            return R.error(500,"发布失败");
        }
        return R.ok("发布成功");
    }
    //根据userId查询当前任务
    @PostMapping("/getTaskByuserId")
    public R getTaskByuserId(int userId){
        List<Task> taskList =taskService.getTaskByuserId(userId);
        for (Task task : taskList) {
            StringBuilder usernamesBuilder = new StringBuilder();
            String member = task.getMember();
            // 使用逗号作为分隔符来分割字符串
            String[] memberIdsStr = member.split(",");
            // 遍历每个成员ID
            for (int i = 0; i < memberIdsStr.length; i++) {
                // 去除可能的空格并转换为int
                int memberId = Integer.parseInt(memberIdsStr[i].trim());
                // 假设userService有一个方法可以根据userId查询用户信息并返回appinfo对象
                appinfo appinfo = userService.findUserByuserId(memberId);
                if (appinfo != null) { // 检查是否找到了用户信息
                    // 将username追加到StringBuilder中，并在非最后一个元素后添加逗号
                    if (usernamesBuilder.length() > 0) {
                        usernamesBuilder.append(",");
                    }
                    usernamesBuilder.append(appinfo.getUserInfo().getUsername());
                }
            }
            String usernames = usernamesBuilder.toString();
            task.setMember(usernames);
        }
        return R.ok(taskList);
    }

    //获取已发布的任务
    @PostMapping("/getpublishedTask")
    public R getpublishedTask(int userId){
        List<publishedTask> publishedTasks = taskService.getpublishedTask(userId);
        for (publishedTask task : publishedTasks) {
            StringBuilder usernamesBuilder = new StringBuilder();
            String member = task.getMember();
            // 使用逗号作为分隔符来分割字符串
            String[] memberIdsStr = member.split(",");
            // 遍历每个成员ID
            for (int i = 0; i < memberIdsStr.length; i++) {
                // 去除可能的空格并转换为int
                int memberId = Integer.parseInt(memberIdsStr[i].trim());
                // 假设userService有一个方法可以根据userId查询用户信息并返回appinfo对象
                appinfo appinfo = userService.findUserByuserId(memberId);
                if (appinfo != null) { // 检查是否找到了用户信息
                    // 将username追加到StringBuilder中，并在非最后一个元素后添加逗号
                    if (usernamesBuilder.length() > 0) {
                        usernamesBuilder.append(",");
                    }
                    usernamesBuilder.append(appinfo.getUserInfo().getUsername());
                }
            }
            String usernames = usernamesBuilder.toString();
            task.setMember(usernames);
        }
        return R.ok(publishedTasks);
    }

    //删除已发布任务
    @PostMapping("/deleteTask")
    public R deleteTask(int taskId){
        try {
            taskService.deleteTask(taskId);
        }catch (Exception e){
            System.out.println("异常为"+e);
            System.out.println("----------------------------");
            return R.error(500,"删除任务失败");
        }
        return R.ok("删除任务成功");
    }
}
